//
//  FlowView.m
//  QuickCure1
//
//  Created by apple on 2016/11/7.
//  Copyright © 2016年 maoge. All rights reserved.
//

#import "FlowView.h"

@interface FlowView ()

@property (weak, nonatomic) IBOutlet UILabel *lb_num;
@property (weak, nonatomic) IBOutlet UIImageView *iv_chat;
@property (weak, nonatomic) IBOutlet UIButton *btn_head;

@property (nonatomic,assign) NSInteger secretNum;
@end


@implementation FlowView

+(instancetype)flowViewWithNIB{
    
    FlowView *flowView = [[NSBundle mainBundle]loadNibNamed:@"FlowView" owner:nil options:nil].lastObject;
    return flowView;
    
}

-(void)awakeFromNib{
    
    [super awakeFromNib];
    
    _lb_num.hidden = YES;
    _iv_chat.hidden = YES;
    _btn_head.alpha = 0.5;
    
    _btn_head.layer.cornerRadius = 35;
    _btn_head.clipsToBounds = YES;
    
    _btn_head.userInteractionEnabled = NO;
    
    [_btn_head setImage:[UIImage imageNamed:@"iq_topic_boy"] forState:UIControlStateNormal];
    [_btn_head setImage:[UIImage imageNamed:@"iq_topic_girl"] forState:UIControlStateHighlighted];
    
    _btn_head.bounds = CGRectMake(0, 0, 70, 70);
    
    
    CAKeyframeAnimation *keyAnim = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    
    
    NSValue *value1 = [NSValue valueWithCATransform3D:CATransform3DIdentity];
    NSValue *value2 = [NSValue valueWithCATransform3D:CATransform3DMakeTranslation(0, 10, 0)];
    NSValue *value3 = [NSValue valueWithCATransform3D:CATransform3DIdentity];
    NSValue *value4 = [NSValue valueWithCATransform3D:CATransform3DMakeTranslation(0, -10, 0)];
    NSValue *value5 = [NSValue valueWithCATransform3D:CATransform3DIdentity];
    
    keyAnim.values = @[
                       value1,value2,value3,value4,value5
                       ];
    keyAnim.duration = 1.5;
    keyAnim.repeatCount = CGFLOAT_MAX;
    [self.layer addAnimation:keyAnim forKey:@"flowKeyAnim"];

    
}



-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    
    
    _btn_head.alpha = 1;
    _btn_head.highlighted = YES;
    _lb_num.hidden = NO;
    _iv_chat.hidden = NO;
    
    _secretNum = arc4random_uniform(10000);
    
    
    
  
    
    
}



-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    UITouch *touch = touches.anyObject;
    
    CGPoint nowLoc = [touch locationInView:touch.view];
    
    CGPoint preLoc = [touch previousLocationInView:touch.view];
    
    
    CGFloat offsetX = nowLoc.x - preLoc.x;
    CGFloat offsetY = nowLoc.y - preLoc.y;
    
    if(self.frame.origin.x + offsetX > - self.width/2 && self.frame.origin.x+self.bounds.size.width + offsetX< SCREEN_WIDTH + self.width/2 && self.frame.origin.y+offsetY > 0 && self.frame.origin.y + self.bounds.size.height + offsetY < SCREEN_HEIGHT){
        self.center = CGPointMake(self.center.x+offsetX, self.center.y+offsetY);
    }
    
}



-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    NSInteger nowNum = _secretNum;
    
    _btn_head.highlighted = NO;
    _lb_num.hidden = YES;
    _iv_chat.hidden = YES;
    
    
    CGFloat newX;
    if(self.center.x > SCREEN_WIDTH/2){
        newX = SCREEN_WIDTH;
    }else{
        newX = 0;
    }
    
    CGFloat newY = self.center.y;
    
    
        
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        if(_secretNum == nowNum){
            [UIView animateWithDuration:0.5 animations:^{
                _btn_head.alpha = 0.5;
                self.center = CGPointMake(newX, newY);
            }completion:^(BOOL finished) {
                
            }];
        }
    });
    
    
    
    
}






@end
